<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="账套" prop="asId">
        <el-select v-model="queryParams.asId" placeholder="请选择查询账套" clearable style="width: 240px" @change="getProductList">
          <el-option v-for="dict in as_list" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="商品" prop="productId">
        <el-select v-model="queryParams.productId" placeholder="请选择商品" style="width: 450px;">
          <el-option v-for="product in productList" :key="product.Id" :label="product.Name"
            :value="product.Id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="数据年份" prop="year">
        <el-date-picker clearable v-model="queryParams.year" type="year" value-format="YYYY" placeholder="请选择数据年份">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['price:packaging:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['price:packaging:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['price:packaging:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="packagingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="数据年份" align="center" prop="year" width="180">
        <template #default="scope">
          <span>{{scope.row.year }}</span>
        </template>
      </el-table-column>
      <el-table-column label="账套" align="center" prop="asId">
        <template #default="scope">
          <dict-tag :options="as_list" :value="scope.row.asId" />
        </template>
      </el-table-column>
      <el-table-column label="关联商品" align="center" prop="productName" />
      <el-table-column label="商品重量（斤）" align="center" prop="productWeight" />
      <el-table-column label="包材费" align="center" prop="materialsCost" />
      <el-table-column label="人工及打冷费" align="center" prop="chillingFee" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['price:packaging:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['price:packaging:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改包装费用管理对话框 -->
    <packagingEdit ref="packagingedit" :productIds="productIds" :productList="productList"
      @closePackaging="closePackaging" />

  </div>
</template>

<script setup name="Packaging">
import { listPackaging, getPackaging, delPackaging, addPackaging, updatePackaging } from "@/api/sales/packaging";
import { listProduct } from "@/api/base/product";
import packagingEdit from './components/packagingEdit.vue'

const { proxy } = getCurrentInstance();
const { as_list, product_area } = proxy.useDict('as_list', 'product_area');

const packagingList = ref([]);
const productList = ref([]);
const packagingId = ref("");
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const productIds = ref([]);
const packagingedit = ref(null);


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    asId: null,
    productId: null,
    year: null,
  },
  rules: {
    materialsCost: [
      { required: true, message: "包材费不能为空", trigger: "blur" }
    ],
    chillingFee: [
      { required: true, message: "人工及打冷费不能为空", trigger: "blur" }
    ],
    productId: [
      { required: true, message: "关联商品不能为空", trigger: "change" }
    ],
    year: [
      { required: true, message: "数据年份不能为空", trigger: "blur" }
    ],
    asId: [
      { required: true, message: "账套不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form } = toRefs(data);

/** 查询包装费用管理列表 */
function getList() {
  loading.value = true;
  listPackaging(queryParams.value).then(response => {
    packagingList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function closePackaging() {
  packagingedit.value.controllDialog(false);
  getList();
  reset();
}


/** 表单重置 */
function reset() {
  form.value = {
    packagingId: null,
    materialsCost: null,
    chillingFee: null,
    productId: null,
    year: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delStatus: null,
  };
  proxy.resetForm("packagingRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据  */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.packagingId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  packagingedit.value.controllDialog(true);
  title.value = "添加包装费用管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  packagingId.value = row.packagingId || ids.value;
  console.log("修改", packagingId.value);
  packagingedit.value.getPackagingDetail(packagingId.value);
  packagingedit.value.controllDialog(true);
  title.value = "修改包装费用管理";
  
}


/** 删除按钮操作 */
function handleDelete(row) {
  const _packagingIds = row.packagingId || ids.value;
  proxy.$modal.confirm('是否确认删除包装费用管理编号为"' + _packagingIds + '"的数据项？').then(function () {
    return delPackaging(_packagingIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}


/** 导出按钮操作 */
function handleExport() {
  proxy.download('price/packaging/export', {
    ...queryParams.value
  }, `packaging_${new Date().getTime()}.xlsx`);
}

/** 是否渲染字段 */
function renderField(insert, edit) {
  return form.value.packagingId == null ? insert : edit;
}

/** 查询商品信息列表 */
function getProductList(e) {
  loading.value = true;
  let asId='200012857'
  if (e) {
    asId = e
  }
  let productQueryParams = {
    pageNum: 1,
    pageSize: 5000,
    keywords: "",
    asId: asId
  };
  listProduct(productQueryParams).then(response => {
    productList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}


getList();
getProductList();
</script>